﻿var viewModel = function () {
    var self = this;
    this.form = {
        usercode: ko.observable(),
        password: ko.observable(),
        remember: ko.observable(false),
        theme: ko.observable("bootstrap"),//默认的语言主题，以后需要修改
        locale: ko.observable("zh_CN")
    };
    this.message = ko.observable();
    this.loginClick = function (form) {
        if (!self.form.password())
            self.form.password($('[type=password]').val());
        $.ajax({
            type: "POST",
            url: "/login/Valid",
            data: ko.toJSON(self.form),
            dataType: "json",
            contentType: "application/json",
            success: function (resp) {
                if (resp.Status == 'Success') {
                    self.message(resp.Message);
                    window.location.href = '/';
                } else {
                    self.message(resp.Message);
                }
            },
            error: function (e) {
                var msg = e.responseText.length > 20 ? '登陆出现未知错误，请重试！' : e.responseText;
                $("#message-div").attr("class", "alert-danger");
                self.message(msg);
            },
            beforeSend: function () {
                $(form).find("input").attr("disabled", true);
                $("#message-div").attr("class", "alert-info");
                self.message("正在登陆处理，请稍候...");
            },
            complete: function () {
                $(form).find("input").attr("disabled", false);
            }
        });
    };

    this.resetClick = function () {
        self.form.usercode("");
        self.form.password("");
        self.form.remember(false);
    };
};

$(function () { ko.applyBindings(new viewModel()); });